from mysqldb.main import *

from .FilterBackends import *


class CQ8BarMemBerInvestListSerializer(serializers.Serializer):
    """无序列化"""

    paper_id = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="会员身份ID"
    )
    UserID = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="会员IDCODE"
    )
    u_AMOUNT = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="金额"
    )
    busi_type = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="类型"
    )
    OP_NAME = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="来源"
    )
    create_time_unix = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="时间戳"
    )


class CQ8BarMemBerUpDownListSerializer(serializers.Serializer):
    """无序列化"""

    create_time = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="时间戳"
    )
    UserID = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="会员IDCODE"
    )
    prepaid_amount = CharField(allow_blank=True, allow_null=True, required=False)
    upmachine_time = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="上机时间戳"
    )
    area_name = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="终端区域"
    )
    fee = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="区域费率(分)"
    )
    ClientIP = CharField(allow_blank=True, allow_null=True, required=False)
    SUM_AMOUNT = CharField(allow_blank=True, allow_null=True, required=False)
    COUNT_AMOUNT = CharField(allow_blank=True, allow_null=True, required=False)
    COUNT_xuMoney = CharField(allow_blank=True, allow_null=True, required=False)
    Max_dtTime = CharField(allow_blank=True, allow_null=True, required=False)
    paper_id = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="会员识别码(场所内唯一)"
    )
    # ------------------共有部分-------------------------
    real_name = CharField(
        allow_blank=True, allow_null=True, required=True, help_text="姓名"
    )
    cn_account_level = CharField(
        allow_blank=True, allow_null=True, required=True, help_text="会员等级"
    )
    # ---------------------------------------------------
    paper_Id = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="会员识别码(场所内唯一)"
    )
    UID = CharField(allow_blank=True, allow_null=True, required=False)
    client_name = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="终端机号"
    )
    client_area = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="终端区域"
    )
    pmode = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="计费类型"
    )

    amount = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="消费金额"
    )
    balance = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="剩余金额"
    )
    timelen = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="上机时长"
    )
    start_time = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="上机时间"
    )
    end_time = CharField(
        allow_blank=True, allow_null=True, required=False, help_text="下机时间"
    )
    pvalue = CharField(allow_blank=True, allow_null=True, required=False)
    svalue = CharField(allow_blank=True, allow_null=True, required=False)


# class CQ8BarMemBerInvestSerializer(serializers.Serializer):
#     """消费充值序列化"""

#     WBCode = CharField(allow_blank=True, allow_null=True, help_text="场所编码")
#     iCount = IntegerField(allow_null=True, help_text="标记")
#     # data = CQ8BarMemBerInvestListSerializer(allow_empty=True, help_text="数据列表")
#     data = ListField(child=CQ8BarMemBerInvestListSerializer(), help_text="数据列表")

#     class Meta:
#         fields = "__all__"


class CQ8BarMemBerUpDownSerializer(serializers.Serializer):
    """上下机序列化"""

    WBCode = CharField(allow_blank=True, allow_null=True, help_text="场所编码")
    iCount = IntegerField(allow_null=True, help_text="标记")
    # data = CQ8BarMemBerUpDownListSerializer(allow_empty=True, help_text="数据列表")
    data = ListField(child=CQ8BarMemBerUpDownListSerializer(), help_text="数据列表")

    class Meta:
        fields = "__all__"


class AllinpayMemberListSerializer(serializers.ModelSerializer):
    """本地会员序列化"""

    id = IntegerField(read_only=True)
    gender = IntegerField(
        allow_null=True, max_value=2147483647, min_value=-2147483648, required=False
    )
    birthday = CharField(
        allow_blank=True, allow_null=True, max_length=255, required=False
    )
    level = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
    name = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
    idcode = CharField(
        allow_blank=True, allow_null=True, max_length=255, required=False
    )
    phone = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
    openid = CharField(
        allow_blank=True, allow_null=True, max_length=255, required=False
    )
    unid = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
    email = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
    avatar = CharField(
        allow_blank=True, allow_null=True, max_length=255, required=False
    )
    hash = CharField(allow_blank=True, allow_null=True, max_length=255, required=True)
    creattime = DateTimeField(allow_null=True, required=False)
    lasttime = DateTimeField(allow_null=True, required=False)
    pid = IntegerField(
        allow_null=True, max_value=2147483647, min_value=-2147483648, required=True
    )
    overage = IntegerField(
        allow_null=True, max_value=2147483647, min_value=-2147483648, required=False
    )
    reward = IntegerField(
        allow_null=True, max_value=2147483647, min_value=-2147483648, required=False
    )
    integral = IntegerField(
        allow_null=True, max_value=2147483647, min_value=-2147483648, required=False
    )
    accummoney = IntegerField(
        allow_null=True, max_value=2147483647, min_value=-2147483648, required=False
    )
    accumtime = IntegerField(
        allow_null=True, max_value=2147483647, min_value=-2147483648, required=False
    )

    # accode = SerializerMethodField()

    class Meta:
        model = AllinpayMemberList
        fields = "__all__"
        validators = [
            UniqueTogetherValidator(
                queryset=AllinpayMemberList.objects.all(), fields=("pid", "hash")
            )
        ]

    def get_accode(self, obj):
        if not obj.openid:
            return "url_code"
        else:
            return None


class AllinpayProgrunListSerializer(serializers.ModelSerializer):
    """8圈在线终端序列化"""

    id = IntegerField(read_only=True)
    message_type = ChoiceField(
        choices=("downmachine", "upmachine"),
        allow_blank=True,
        allow_null=True,
        required=False,
    )
    carid = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
    guid = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
    computer_name = CharField(
        allow_blank=True, allow_null=True, max_length=255, required=False
    )
    computer_area = CharField(
        allow_blank=True, allow_null=True, max_length=255, required=False
    )
    bem = CharField(allow_blank=True, allow_null=True, max_length=255, required=False)
    amount = IntegerField(
        allow_null=True, max_value=2147483647, min_value=-2147483648, required=False
    )
    pid = IntegerField(
        allow_null=True, max_value=2147483647, min_value=-2147483648, required=False
    )
    on_day = DateField(allow_null=True, required=False)
    on_time = DateTimeField(allow_null=True, required=False)
    down_day = DateField(allow_null=True, required=False)
    down_time = DateTimeField(allow_null=True, required=False)

    MemberInfo = SerializerMethodField()

    class Meta:
        model = AllinpayProgrunList
        fields = "__all__"

    def get_MemberInfo(self, obj):
        """子查询"""
        try:
            res = AllinpayMemberList.objects.get(pid=obj.pid, idcode=obj.carid)
        except BaseException as err:
            print(err)
            return None
        else:
            return AllinpayMemberListSerializer(res).data

class TestNoneSerializer(serializers.Serializer):
    '''无序列化'''
    WBCode = CharField(allow_blank=True, allow_null=True, help_text="场所编码")
    name = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=255,
        required=True,
        help_text="计算机名"    )    
    data = CharField(
        allow_blank=True,
        allow_null=True,
        max_length=1024*20,
        required=True,
        help_text="JSON数据"    ) 
    class Meta:
        fields = '__all__'
        
        
        
        
     
        
# print(repr(GameLolHonebaoListSerializer()))
